<template>
  <div>
    <!-- 体温记录 -->
    <!-- 搜索 -->
    <el-card class="card">
      <el-form ref="ruleFormRef" :inline="true" class="demo-form-inline" :model="params">
        <el-form-item label="老人：" prop="name">
          <el-input v-model="params.name" clearable placeholder="请输入" style="width: 150px" />
        </el-form-item>
        <el-form-item label="床位：" prop="begId">
          <el-cascader
            v-model="params.begId"
            :props="props"
            :options="getcounts"
            clearable
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="查询时间：" prop="dateTime">
          <el-date-picker
            v-model="dateTime"
            type="daterange"
            unlink-panels
            range-separator="-"
            start-placeholder="开始时间"
            value-format="YYYY-MM-DD"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            @change="(e: any) => dateChange(e, params)"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="card">
      <ELTable v-bind="tableConfig" ref="bloodTable">
        <template #addTime="{ scope }">
          {{ dayJs(scope.row.addTime).format("YYYY-MM-DD HH:mm") }}
        </template>
        <template #operate="{ scope, deleteHandle }">
          <el-button type="primary" :icon="Edit" text @click="update(scope.row.id)">编辑</el-button>
          <el-button type="danger" :icon="Delete" text @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
        <template #btns="{ deleteAll }">
          <el-button type="success" :icon="Plus" @click="add">添加</el-button>
          <el-button :icon="Delete" type="danger" @click="deleteAll">批量删除</el-button>
        </template>
      </ELTable>
    </el-card>
    <AddTemperature v-if="dialogVisible" :updateId="updateId" @close="closeHandle" />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { ListParams } from "@/api/medicalcare/temperature/type";
import {
  delTemperature,
  delTemperatureMany,
  getTemperatureList,
} from "@/api/medicalcare/temperature/temperature";
import { bedList } from "@/api/config/bed/bed";
import { buildTree } from "@/utils/treeUtil";
import { badListParams } from "@/api/config/bed/type";
import { houseList } from "@/api/config/house/house";
import { listParams } from "@/api/config/house/type";
import { buildingListData } from "@/api/config/building/type";
import { buildingList } from "@/api/config/building/building";
import { Document, Delete, Edit, Search, Refresh, Plus } from "@element-plus/icons-vue";
import ELTable, { TableData } from "@/components/ELTable.vue";
import dayJs from "dayjs";
import AddTemperature from "@/components/medicalcare/temperature/AddTemperature.vue";

const bloodTable = ref();
const ruleFormRef = ref<FormInstance>();
//添加dialog
const dialogVisible = ref(false);
//级联选择器格式转换
const props = { label: "name", value: "id" };

// 楼栋列表
const buildList = ref<buildingListData[]>([]);
const getBuildingList = async () => {
  let res = await buildingList();
  buildList.value = res.list;
  getHouseList();
};

//房间列表
const houseParams = reactive<listParams>({
  page: 1,
  pageSize: 1000,
  name: "",
  buildingId: undefined,
});
const house_list = ref<any[]>([]);
const getHouseList = async () => {
  let res = await houseList(houseParams);
  house_list.value = res.list.map((item) => ({
    id: item.id,
    name: item.name,
    pid: item.buildingId,
  }));
  getBedList();
};
//床位
const bedParams = reactive<badListParams>({
  houseId: undefined,
  page: 1,
  pageSize: 1000,
});
const bedListData = ref<any>([]);
const getBedList = async () => {
  let res = await bedList(bedParams);
  bedListData.value = res.list.map((item) => ({
    id: item.id,
    name: item.name,
    pid: item.houseId,
  }));
  mergeData([...buildList.value, ...house_list.value, ...bedListData.value]);
  // console.log("1111",bedListData.value);
};

//选择床位
const handleChange = (data: any) => {
  // console.log("7777data",data);

  if (data) {
    bedParams.houseId = data[data.length - 1];
    params.begId = data[data.length - 1];
    getBedList();
  }
};

// 合并
const getcounts = ref([]);
const mergeData = (data: any) => {
  getcounts.value = buildTree(data);
  // console.log("getCounts999",getcounts.value);
};
//创建日期
const dateTime = ref([]);
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "最近3个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];

//选择时间
const dateChange = (e: any, params: any) => {
  if (e) {
    params.beginDate = e[0];
    params.endDate = e[1];
  } else {
    params.beginDate = "";
    params.endDate = "";
  }
};

//搜索参数
const params = reactive<ListParams>({
  name: "",
  beginDate: "",
  endDate: "",
  begId: undefined,
});

//重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dateTime.value = [];
};

//搜索
const search = () => {
  bloodTable.value.getData(params);
};
//表格数据
const tableConfig = reactive<TableData>({
  loadData: getTemperatureList,
  delete: delTemperature,
  deleteAll: delTemperatureMany,
  columns: [
    {
      prop: "id",
      label: "序号",
    },
    {
      prop: "addTime",
      label: "统计日期",
      slot: true,
    },
    {
      prop: "addAccountName",
      label: "记录人",
    },
    {
      prop: "elderlyName",
      label: "老人姓名",
    },
    {
      prop: "begName",
      label: "床位",
    },
    {
      prop: "val",
      label: "体温",
    },

    {
      prop: "operate",
      label: "操作",
      slot: true,
      attrs: {
        width: 200,
      },
    },
  ],
});
//添加
const add = () => {
  updateId.value = 0;
  dialogVisible.value = true;
};

const updateId = ref(0);
//编辑
const update = (id: number) => {
  updateId.value = id;

  dialogVisible.value = true;
};

//关闭dialog
const closeHandle = (data: boolean) => {
  dialogVisible.value = false;
  if (data) {
    //刷新
    bloodTable.value.getData();
  }
};
onMounted(() => {
  getBuildingList();
});
</script>

<style scoped>
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>
